package com.yixin.hospital.controller;

import cn.hutool.core.lang.tree.Tree;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.dept.DeptAddReqDTO;
import com.yixin.hospital.entity.dto.req.dept.DeptEditReqDTO;
import com.yixin.hospital.entity.dto.req.dept.TempDeptAddReqDTO;
import com.yixin.hospital.service.DeptService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门管理", tags = "部门管理")
@Slf4j
public class DeptController {

    @Resource
    private DeptService deptService;

    @GetMapping("/list")
    @ApiOperation(value = "部门列表（树状）")
    public ApiResult<List<Tree<Integer>>> list() {
        return deptService.treeList();
    }

    @GetMapping("/dept-drop-list")
    @ApiOperation(value = "部门下拉框")
    public ApiResult deptDropList(@RequestParam(value = "page", required = false) Integer page,
                                  @RequestParam(value = "size", required = false, defaultValue = "300") Integer size) {
        return deptService.deptDropList(page, size);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加部门")
    public ApiResult add(@Valid @RequestBody DeptAddReqDTO deptAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return deptService.add(deptAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑部门")
    public ApiResult edit(@Valid @RequestBody DeptEditReqDTO deptEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return deptService.edit(deptEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除部门")
    public ApiResult remove(@PathVariable int id) {
        deptService.removeById(id);
        return ApiResult.success();
    }

    @PostMapping("/add-temp-dept")
    @ApiOperation(value = "临时部门申请")
    public ApiResult<?> addTempDept(@Valid @RequestBody TempDeptAddReqDTO tempDeptAddReqDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return deptService.addTempDept(tempDeptAddReqDTO, userId, userName);
    }

    @GetMapping("/user-temp-dept-list")
    @ApiOperation(value = "临时部门申请记录(自己看)")
    public ApiResult<?> userTempDeptList(@RequestParam int currentPage, @RequestParam int pageSize, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return deptService.tempDeptList(currentPage, pageSize, userId);
    }

    @GetMapping("/all-user-temp-dept-list")
    @ApiOperation(value = "临时部门申请记录(管理员看)")
    public ApiResult<?> allUserTempDeptList(@RequestParam int currentPage, @RequestParam int pageSize, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return deptService.allTempDeptList(currentPage, pageSize, userId);
    }
}
